package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AssertUtil;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserCollect;
import com.atguigu.tingshu.model.user.UserSubscribe;
import com.atguigu.tingshu.user.mapper.UserCollectMapper;
import com.atguigu.tingshu.user.mapper.UserSubscribeMapper;
import com.atguigu.tingshu.user.service.UserCollectService;
import com.atguigu.tingshu.user.service.UserSubscribeService;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.user.UserCollectVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements UserCollectService {

    @Autowired
    private UserCollectMapper userCollectMapper;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;


    // 分页查询用户收藏的声音列表
    @Override
    public Page<UserCollectVo> findUserCollectPage(Long pageNum, Long pageSize) {
       // 分页查询基本信息
        Page<UserCollect> page = userCollectMapper.selectPage(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, AuthContextHolder.getUserId())
                .orderByDesc(UserCollect::getCreateTime));


        // 远程调用根据专辑id查询专辑的信息
        List<UserCollect> userCollects = page.getRecords();
        List<UserCollectVo> userCollectVoList = userCollects.stream().map(userCollect -> {
            UserCollectVo userCollectVo = new UserCollectVo();
            userCollectVo.setTrackId(userCollect.getTrackId());
            userCollectVo.setCreateTime(userCollect.getCreateTime());

            // 远程调用根据声音id查询专辑的信息
            Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfoInner(userCollect.getTrackId());
            AssertUtil.resultAssert(trackInfoResult, ResultCodeEnum.DATA_ERROR);
            TrackInfo trackInfo = trackInfoResult.getData();
            Result<AlbumListVo> albumInfoResult = albumInfoFeignClient.getAlbumInfoById(trackInfo.getAlbumId());
            AssertUtil.resultAssert(albumInfoResult, ResultCodeEnum.DATA_ERROR);
            AlbumListVo albumListVo = albumInfoResult.getData();
            userCollectVo.setAlbumId(albumListVo.getAlbumId());
            userCollectVo.setTrackTitle(trackInfo.getTrackTitle());
            userCollectVo.setCoverUrl(albumListVo.getCoverUrl());
            return userCollectVo;
        }).toList();


        Page<UserCollectVo> userCollectVoPage = new Page<>();
        userCollectVoPage.setTotal(page.getTotal());
        userCollectVoPage.setPages(page.getPages());
        userCollectVoPage.setCurrent(page.getCurrent());
        userCollectVoPage.setSize(page.getSize());
        userCollectVoPage.setRecords(userCollectVoList);
        return userCollectVoPage;
    }
}
